package com.founder.sample;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TodoItemAdapter extends ArrayAdapter<TodoItem> {
	
	int textViewResourceId;
	
	public TodoItemAdapter(Context context, int textViewResourceId, List<TodoItem> items) {
		super(context, textViewResourceId, items);
		this.textViewResourceId = textViewResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		LinearLayout todoView;
		
		TodoItem item = getItem(position);
		
		String taskString = item.getTask();
		Date createDate = item.getCreated();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = df.format(createDate);
		
		if (convertView == null) {
			todoView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(textViewResourceId, todoView, true);
		} else {
			todoView = (LinearLayout)convertView;
		}
		
		TextView dateView = (TextView)todoView.findViewById(R.id.rowDate);
		TextView taskView = (TextView)todoView.findViewById(R.id.row);
		
		dateView.setText(dateString);
		taskView.setText(taskString);
		
		return todoView;
	}

}
